package com.google.agerasample.slice;

import com.google.agerasample.DisplayUtil;
import com.google.agerasample.ResourceTable;
import com.google.agerasample.basicsample.ListHolder;
import com.google.agerasample.basicsample.OnClickObservable;
import com.google.agerasample.basicsample.UsernamesFetcher;
import com.google.agerasample.basicsample.UsernamesRepository;
import com.google.agera.Updatable;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.ToastDialog;

public class BasicsamAbilitySlice extends AbilitySlice implements Updatable {
    private ListHolder listHolder;
    private Button button;

    private OnClickObservable clickObservable =
            new OnClickObservable() {
                @Override
                public void onClick(Component component) {
                    super.onClick(component);
                    showLoadingDialog();
                }
            };

    private UsernamesRepository usernamesRepository;
    private CommonDialog commonDialog;

    private void showLoadingDialog() {
        if (commonDialog == null) {
            commonDialog = new CommonDialog(this);
            int windowHeight = DisplayUtil.getScreenHeight(this);
            int windowWidth = DisplayUtil.getScreenWidth(this);
            if (windowHeight > windowWidth) {
                commonDialog.setSize(windowWidth / 2, windowHeight / 6);
            } else {
                commonDialog.setSize(windowWidth / 3, windowHeight / 2);
            }
            commonDialog.setContentText("loading");
            commonDialog.setCornerRadius(8);
            commonDialog.show();
        }
    }

    @Override
    public void onStart(Intent intent) {
        listHolder = new ListHolder(this);
        setUIContent(listHolder.createComponent());

        button = (Button) listHolder.rootLayout.findComponentById(ResourceTable.Id_btn);
        button.setClickedListener(clickObservable);
        // Initialise the repository
        usernamesRepository = new UsernamesRepository(new UsernamesFetcher());

        showLoadingDialog();
    }

    @Override
    public void onActive() {
        super.onActive();
        // We make sure the repository observes the refresh listener
        clickObservable.addUpdatable(usernamesRepository);

        /**
         * We make sure the main fragment observes the repository. This will also trigger the
         * repository to update itself, via
         * {@link UsernamesRepository#firstUpdatableAdded(UpdateDispatcher)}.
         */
        usernamesRepository.addUpdatable(this);

        /**
         * We update the UI to show the data is being updated. We need to wait for the
         * {@link swipeRefreshLayout} to be ready before asking it to show itself as refreshing.
         */
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        // We remove the observations to avoid triggering updates when they aren't needed
        clickObservable.removeUpdatable(usernamesRepository);
        usernamesRepository.removeUpdatable(this);
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void update() {
        // Check error status
        if (usernamesRepository.isError()) {
            // Show error message, do not update list as we still want to show the last known list of
            // usernames
            new ToastDialog(this).setText("Error refreshing usernames").show();
        } else {
            // Update the list of usernames
            listHolder.listItemProvider.setData(usernamesRepository.get());
            listHolder.listItemProvider.notifyDataChanged();
        }
        if (commonDialog != null) {
            commonDialog.hide();
            commonDialog.destroy();
            commonDialog = null;
        }
    }
}
